import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'
import { getToken } from '@/utils/auth'
import store from '@/store'
import { Message } from 'element-ui'

const whiteList = ['/login', '/register']

export const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login/index'),
        hidden: true
    },

    {
        path: '/404',
        component: () => import('@/views/404'),
        hidden: true
    },

    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [{
            path: 'dashboard',
            name: 'Dashboard',
            component: () => import('@/views/dashboard/index'),
            meta: { title: '首页', icon: 'dashboard' }
        }]
    },
    {
        path: '/manage',
        component: Layout,
        redirect: '/manage',
        name: 'Manage',
        meta: { title: '系统管理', icon: 'el-icon-s-tools' },
        children: [
            {
                path: 'oss',
                name: 'Oss',
                component: () => import('@/views/oss'),
                meta: { title: '文件管理', icon: 'el-icon-upload' }
            },{
                path: 'ossConfig',
                name: 'ossConfig',
                component: () => import('@/views/oss/config.vue'),
                meta: { title: '存储配置', icon: 'el-icon-menu' }
            },{
                path: 'operlog',
                name: 'operlog',
                component: () => import('@/views/monitor/operlog'),
                meta: { title: '操作日志', icon: 'el-icon-menu' }
            },
        ]
    },
    {
        path: '/example',
        component: Layout,
        redirect: '/example',
        name: 'Example',
        meta: { title: '班级学员管理', icon: 'el-icon-s-help' },
        children: [
            {
                path: 'classes',
                name: 'Classes',
                component: () => import('@/views/classes'),
                meta: { title: '班级管理', icon: 'el-icon-menu' }
            },
            {
                path: 'student',
                name: 'Student',
                component: () => import('@/views/student'),
                meta: { title: '学员管理', icon: 'el-icon-user-solid' }
            }
        ]
    }, {
        path: '/system',
        component: Layout,
        redirect: '/system',
        name: 'System',
        meta: { title: '系统信息管理', icon: 'el-icon-s-tools' },
        children: [
            {
                path: 'dept',
                name: 'Dept',
                component: () => import('@/views/dept'),
                meta: { title: '部门管理', icon: 'el-icon-menu' }
            },
            {
                path: 'emp',
                name: 'Emp',
                component: () => import('@/views/emp'),
                meta: { title: '员工管理', icon: 'el-icon-user-solid' }
            }
        ]
    },
    {
        path: '/report',
        component: Layout,
        redirect: '/report',
        name: 'Report',
        meta: { title: '数据统计管理', icon: 'el-icon-s-tools' },
        children: [
            {
                path: 'emp-report',
                name: 'emp-report',
                component: () => import('@/views/emp-report'),
                meta: { title: '员工信息统计', icon: 'el-icon-s-data' }
            },
            {
                path: 'student-report',
                name: 'student-report',
                component: () => import('@/views/student-report'),
                meta: { title: '学员信息统计', icon: 'el-icon-s-data' }
            }
        ]
    },
    {
        path: '/message',
        component: Layout,
        redirect: '/message',
        children: [{
            path: 'message',
            name: 'message',
            component: () => import('@/views/message'),
            meta: { title: '通知', icon: 'el-icon-s-comment' }
        }]
    },
    {
        path: '/ai-chat',
        component: Layout,
        redirect: '/ai-chat',
        children: [{
            path: 'ai-chat',
            name: 'ai-chat',
            component: () => import('@/views/ai-chat/index.vue'),
            meta: { title: 'AI对话', icon: 'el-icon-s-comment' }
        }]
    },
    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()

router.beforeEach((to, from, next) => {
    if (getToken() && to.path !== '/login') {
        // 已登录的情况下访问登录页重定向到首页
        if(to.path === '/login') {
            next({path: '/'})
        }
        else {
            if (!store.state.user.name) {
                console.log(111)
                store.dispatch('user/getInfo')
                  .then(response => {
                    next()
                  })
                  .catch(e => {
                      store.dispatch('user/logout').then(() => {
                          Message.error(e)
                          next("/login")
                      })
                  })
                
            }
            else {
                next()
            }
        }
    }
    else {
        if (whiteList.indexOf(to.path) !== -1) {
            // 在免登录白名单，直接进入
            next()
        } else {
            next(`/login}`)
        }
    }
    
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
